import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo3/entity/news_entity.dart';
import 'package:flutter_demo3/entity/user_entity.dart';
import 'package:flutter_demo3/util/dio_util.dart';

class NewsListItem extends StatefulWidget {
  List<NewsData> _newsDatas;
  List<UserData> _userDatas;
  int _index;

  NewsListItem(this._newsDatas, this._userDatas, this._index);

  @override
  _NewsListItemState createState() =>
    _NewsListItemState(this._newsDatas, this._userDatas, this._index);
}

class _NewsListItemState extends State<NewsListItem> {
  List<NewsData> _newsDatas;
  List<UserData> _userDatas;
  int _index;
  Color _backgroundColor = Colors.white;

  _NewsListItemState(this._newsDatas, this._userDatas, this._index);

  @override
  Widget build(BuildContext context) {
    return _allBody();
  }

  Widget _allBody() {
    return InkWell(
      child: Padding(
        padding: const EdgeInsets.fromLTRB(0, 0, 0, 3),
        child: Container(
          decoration: BoxDecoration(
              color: _backgroundColor, borderRadius: BorderRadius.circular(5)),
          width: MediaQuery
              .of(context)
              .size
              .width,
          height: 140,
          child: Column(
            children: <Widget>[
              Expanded(
                child: _itemBar(),
                flex: 1,
              ),
              Expanded(
                child: _itemBody(),
                flex: 3,
              ),
            ],
          ),
        ),
      ),
      onTapDown: (down) {
        setState(() {
          _backgroundColor = Colors.white10;
        });
      },
      onTap: () {
        setState(() {
          _backgroundColor = Colors.white;
        });
      },
    );
  }

  Widget _itemBar() {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [BoxShadow(color: Colors.black26, blurRadius: 5)],
          borderRadius: BorderRadius.circular(5)
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(10, 0, 5, 0),
            child: ClipOval(
              child: Image.network(
                DioUtil.head_url + this._userDatas[_index].headImg,
                fit: BoxFit.cover,
                height: 20,
                width: 20,
              ),
            ),
          ),
          Text(
            this._userDatas[_index].name,
            style: TextStyle(
                fontSize: 15,
                color: Colors.black54,
                fontWeight: FontWeight.normal),
          )
        ],
      ),
    );
  }

  Widget _itemBody() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        //图片
        Padding(
          padding: const EdgeInsets.all(3.0),
          child: ClipRRect(
            child: Image.network(
              DioUtil.head_url + this._newsDatas[_index].imgUrl,
              fit: BoxFit.cover,
              width: 125,
              height: 100,
            ),
          ),
        ),

        Expanded(
          flex: 1,
          child: Container(
            height: 90,
            child: Stack(
              children: <Widget>[
                Align(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        //标题
                        Text(
                          this._newsDatas[_index].title,
                          style: TextStyle(
                              fontSize: 17,
                              color: Colors.black87,
                              fontWeight: FontWeight.bold),
                          overflow: TextOverflow.ellipsis,
                        ),

                        //内容简介
                        Text(
                          this._newsDatas[_index].content,
                          style: TextStyle(fontSize: 11, color: Colors.black45),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 2,
                        ),
                      ],
                    ),
                    alignment: Alignment.topLeft),

                //时间
                Align(
                  child: Text(
                    this._newsDatas[_index].date,
                    style: TextStyle(fontSize: 10, color: Colors.black26),
                  ),
                  alignment: Alignment.topRight,
                ),

                //点赞
                Align(
                    child: Padding(
                      padding: const EdgeInsets.all(3.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: <Widget>[
                          Icon(
                            Icons.favorite_border,
                            size: 16,
                          ),
                          Text(this._newsDatas[_index].likesNum.toString())
                        ],
                      ),
                    ),
                    alignment: Alignment.bottomLeft)
              ],
            ),
          ),
        )
      ],
    );
  }
}